package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.dao.CarouselDao;
import com.book.entity.Carousel;
import com.book.service.CarouselService;
import com.book.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//错误码以11001开始
@Service
public class CarouselServiceImpl implements CarouselService {
    @Autowired
    private CarouselDao carouselDao;

    @Override
    public ResponseResult getCarousel() {
        try {
            QueryWrapper<Carousel> qw = new QueryWrapper<>();
            qw.orderByDesc("id");
            return new ResponseResult(200,"获取成功",carouselDao.selectList(qw));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(11001,"获取轮播图失败");
        }
    }

    @Override
    public ResponseResult getCarouselOne(int id) {
        try {
            return new ResponseResult(200,"获取成功",carouselDao.selectById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(11002,"获取此图片信息失败");
        }
    }

    @Override
    public ResponseResult deleteCarousel(int id) {
        try {
            return new ResponseResult(200,"删除成功",carouselDao.deleteById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(11003,"删除失败");
        }
    }


    /**
     * 编辑或添加轮播图
     * @param carousel
     * @return
     */
    @Override
    public ResponseResult saveCarousel(Carousel carousel) {
        try {
            if(carousel.getId()==0){
                //说明是添加
                carouselDao.insert(carousel);
            }
            else {
                //说明是更新
                carouselDao.updateById(carousel);
            }
            return new ResponseResult(200,"保存成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(11004,"保存失败");
        }
    }


}
